﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Service;
using Resources;

public partial class supplierView : System.Web.UI.Page
{
    readonly StockServiceSupplier _stockServiceSupplier = new StockServiceSupplier();
    private readonly DirectServiceSupplier _directServiceSupplier = new DirectServiceSupplier();
    protected void Page_Load(object sender, EventArgs e)
    {
        // lblTotalSoftware.Text = "Total Software:" + _ReferenceDataService.GetSoftwares().Count;
        // lblTotalSupplier.Text = "Total supplier : " + new DirectServiceSupplier().GetSupplierDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
        
        if (Session["whereClause"] == null)
            Session["whereClause"] = txtHiddenWhereClause.Text;
        if (Session["orderByClause"] == null)
            Session["orderByClause"] = txthiddenOrderbyClause.Text;

        //grdSupplier.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;

        new Utility().SetRowToDisplay(grdSupplier, Session["rowsToSDisplay"]);

        if (!IsPostBack)
        {
            // to avoid loading for the first time
             
            if ((Request.QueryString["detail"] == null))
            {

                if ((Request.QueryString["delete"] == null))
                {  
    
                }
                else
                {
                    if (!new Utility().TestPermissions(Resource.PermissionDeleteSupplier))
                    {
                        lblPermission.Text = Resource.Permission;
                        return;
                    }
                    lblPermission.Visible = false;
                    int companyId = Request.QueryString["companyId"] != null ? Int32.Parse(Request.QueryString["companyId"]) : 0;
                  
                    _stockServiceSupplier.DeleteSupplier(companyId);
                   
                    lblTotalSupplier.Text = "Total supplier : " +new DirectServiceSupplier().GetSupplierDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
                }
            }


            if (Session["pageIndex"] != null)
            {
                grdSupplier.PageIndex = Int32.Parse(Session["pageIndex"].ToString());

            }

            /* 
             * 1. number of equipments
             * 2. select the element 
             * */
            

        }
        else
        {
            //  SystemList = new DirectService().DataTableToIlistSystem(GetWhereClause(), GetOrderClause());
            txtHiddenPageIndex.Text = "0";
            lblTotalSupplier.Text = "Total supplier : " + new DirectServiceSupplier().GetSupplierDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            int number = +new DirectServiceSupplier().GetSupplierDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            if (number == 0)
            {
                grdSupplier1 .Visible = true;
            }
            else
            {
                grdSupplier.Visible = true;
                grdSupplier1.Visible = false;
            }
        }
        grdSupplier.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;
    }

    public int GetSupplierId()
    {
        return !String.IsNullOrEmpty(Request.QueryString["companyId"]) ? Int32.Parse(Request.QueryString["companyId"]) : 0;
    }
    protected string GetWhereClause()
    {
        string whereClause = string.Empty;
        string and = "";
        string str = "";

        // Prepare by equipement
        int i = 0;
        if (!string.IsNullOrEmpty(txtSuplierId.Text))
        {
            if (int.TryParse(txtSuplierId.Text, out i))
            {
                and = " and ";
                str = " where CompanyConcerning.CompanyId=" + Int32.Parse(txtSuplierId.Text);
                if (txtSupplierName.Text != string.Empty)
                {
                    str += " " + "and" + " " + "CompanyConcerning.CompanyName=" + "'" + txtSupplierName.Text + "'";
                    and = " and ";
                }
            }
            else
            {
                grdSupplier.Visible = false;
                grdSupplier1.Visible = true;
                lblTotalSupplier.Text = "Total system: 0";
                return null;
            }
        }
        else
        {
            if (txtSupplierName.Text != string.Empty)
            {
                str += " " + "where" + " " + "CompanyConcerning.CompanyName like" + "'%" + txtSupplierName.Text + "%'";
                and = " and ";
            }
        }
        if(string.IsNullOrEmpty(txtSuplierId.Text) && string.IsNullOrEmpty(txtSupplierName.Text) && !string.IsNullOrEmpty(txtVat.Text))
        {
            str += " " + "where" + " " + "CompanyConcerning.Vat=" + float.Parse(txtVat.Text);
            and = " and ";
        }
        if(string.IsNullOrEmpty(txtSuplierId.Text) && string.IsNullOrEmpty(txtSupplierName.Text) && !string.IsNullOrEmpty(txtPhone.Text))
        {
            str += " " + "where" + " " + "CompanyConcerning.Phone=" + "'" + txtPhone.Text + "'";
            and = " and ";
        }
        if(string.IsNullOrEmpty(txtSuplierId.Text) && string.IsNullOrEmpty(txtSupplierName.Text) && dlCategory.SelectedIndex!=0 )
        {
            str += " " + "where" + " " + "CompanyConcerning.CategoryId=" + int.Parse(dlCategory.SelectedItem.Value);
            and = " and ";
        }
        if (!String.IsNullOrEmpty(str))
            whereClause = str + " ";
        return whereClause;
    }
    protected string GetOrderClause()
    {
        return " Order by COMPANYCONCERNING.CompanyName";
    }
    protected void bntFilter_Click(object sender, EventArgs e)
    {

        Session["whereClause"] = GetWhereClause();
        Session["orderByClause"] = GetOrderClause();
        txthiddenOrderbyClause.Text = GetOrderClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;
        ClearControlSearch();




    }
    private void ClearControlSearch()
    {
        txtSuplierId.Text = "";
        txtSupplierName.Text = "";
        txtVat.Text = "";
        txtPhone.Text = "";
      //  txtCategory.Text = ""; 
        dlCategory.SelectedIndex = 0;

    }
    public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }

        return tabIndex;
    }

    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ?
                       Int32.Parse(Session["pageIndex"].ToString())
                   : 0;
    }
    protected void grdSupplier_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        new Utility().SetSelectedColorToGridView(e, grdSupplier);
        //if (e.CommandName.Equals("Select"))
        {

            new Utility().SetSelectedColorToGridView(e, grdSupplier);
            int companyId = Convert.ToInt32(grdSupplier.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
           new Utility().SetSelectedColorToGridView(e, grdSupplier);
           // ValueUseOtherPage.companyId = companyId; 
        }
        if (e.CommandName.Equals("Delete"))
        {
            int supplierId = Convert.ToInt32(grdSupplier.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            _directServiceSupplier.DeteteSupplier(supplierId);
            Response.Redirect("supplierView.aspx");
        }
      
    }
    protected void grdSupplier_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    //block to add message

        //    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
        //    {
        //        LinkButton deleteButton = (LinkButton)e.Row.Cells[1].Controls[0];
        //        deleteButton.Attributes.Add("onclick", "return confirm('" + Resource.ConfirmDeleteMessage + "?');");
        //    }
        //}
    }
    protected void grdSupplier_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        grdSupplier.PageIndex = e.NewPageIndex;
        txtHiddenPageIndex.Text = grdSupplier.PageIndex.ToString();
        Session["pageIndex"] = grdSupplier.PageIndex;

    }
    protected void grdSupplier_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void dsSupplier_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
      //  if (e.ReturnValue != null) lblTotalSupplier.Text = "Total supplier:" + ((DataTable)e.ReturnValue).Rows.Count;
    }
    protected void dsSupplier_Selected1(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue != null) lblTotalSupplier.Text = "Total supplier:" + ((DataTable)e.ReturnValue).Rows.Count;
    }
}
